import { navigateTo, useState } from 'nuxt/app'
import { computed, ref, readonly, onMounted } from 'vue'

export const useAuth = () => {
  const isAuthenticated = useState<boolean>('auth', () => false)
  
  const login = async (email: string, password: string) => {
    try {
      const response = await $fetch<{ success: boolean }>('/api/auth/login', {
        method: 'POST',
        body: { email, password }
      })
      
      if (response.success) {
        isAuthenticated.value = true
        return true
      }
      return false
    } catch (error) {
      console.error('Login error:', error)
      return false
    }
  }
  
  const logout = async () => {
    try {
      await $fetch('/api/auth/logout', {
        method: 'POST'
      })
      isAuthenticated.value = false
    } catch (error) {
      console.error('Logout error:', error)
    }
  }
  
  return {
    isAuthenticated,
    login,
    logout
  }
} 